<?php

namespace App\Http\Controllers\System;

use App\Model\System\AuthPermission;
use App\Model\System\AuthRole;
use App\Model\System\AuthRolePermission;
use App\Model\System\AuthUserRole;
use App\Model\System\Token;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Laravel\Lumen\Routing\Controller as BaseController;
use App\Model\System\User;
use App\Http\Controllers\MessagePacket;

/**
 * eeo account check
 * Class AccountController
 * @package App\Http\Controllers\System
 */
class AuthorityController extends BaseController
{
    use MessagePacket;

    /**
     * 创建新角色
     * @param Request $request
     * @return MessagePacket
     */
    function addRole(Request $request)
    {
       $role =  new AuthRole($request->all());
       if($role->save()){
           return $this->success(true);
       }
       return $this->fail(false);
    }

    /**
     * update role
     * @param Request $request
     * @param $id
     * @return MessagePacket
     */
    function updateRole(Request $request, $id)
    {
        $role = AuthRole::find($id);
        $role->update($request->all());

        if($role->save()){
            return $this->success(true);
        }
        return $this->fail(false);
    }

    /**
     * get one role
     * @param Request $request
     * @param $id
     * @return MessagePacket
     */
    function getRole(Request $request, $id)
    {
        $role = AuthRole::find($id);

        if($role){
            return $this->success($role);
        }
        return $this->fail(false);
    }


    /**
     * 批量获取
     * @param Request $request
     * @return array
     */
    function getRoles(Request $request)
    {
        $query = AuthRole::whereNotNull('id');
        if($request->has('title')){
            $title = $request->get('title');
            $query->where('title', 'like', "%$title%");
        }
        if($request->has('status')){
            $query->where('status', $request->status);
        }
        $roles = $query->get();
        if($roles){
            return $this->success($roles);
        }
        return $this->success([]);
    }

    /**
     * 删除某个角色
     * @param Request $request
     * @param $id
     * @return array
     */
    function delRole(Request $request, $id)
    {
        try{
            $role = AuthRole::find($id);
            AuthUserRole::where('roleid', $id)->delete();
            $role->delete();
            return $this->success(true);
        }catch (\Exception $e){
            return $this->fail(false);
        }
    }


    /**
     * update user role
     * @param Request $request
     * @return array
     * @throws
     */
    function updateUserRoles(Request $request)
    {
        try{
            if($request->has('uid')){
                $uid = $request->get('uid');
            }
            AuthUserRole::where('userid', $uid)->delete();

            if ($request->has('roleids')){
                $roleids = $request->get('roleids');
                foreach ($roleids as $rid){
                    $info = ['userid'=>$uid, 'roleid'=>$rid];
                    $obj = new AuthUserRole($info);
                    $obj->save();
                }
            }
            return $this->success(true);
        }catch (\Exception $e){
            return $this->fail(false, $e->getMessage());
        }
    }

    /**
     * get one's roles
     * @param Request $request
     * @param $id
     * @return array
     */
    function getUserRoles(Request $request, $id)
    {
        try{
            $roleids = AuthUserRole::where('userid', $id)->pluck('roleid')->toarray();
            $roles = AuthRole::find($roleids);
            return $this->success($roles);
        }catch (\Exception $e){
            return $this->fail(false, $e->getMessage());
        }
    }


    /**
     * update role permission
     * @param Request $request
     * @param $rid
     * @return array
     */
    function updateRolePermissions(Request $request)
    {
        try{
            if($request->has('roleid')){
                $rid = $request->get('roleid');
            }

            if($request->has('permissionids')){
                $ids = $request->get('permissionids');
                AuthRolePermission::where('roleid', $rid)->delete();
            }
            foreach ($ids as $pid){
                $info = ['permissionid'=>$pid, 'roleid'=>$rid];
                $obj = new AuthRolePermission($info);
                $obj->save();
            }
            return $this->success(true);
        }catch (\Exception $e){
            return $this->fail(false);
        }
    }

    /**
     * @param Request $request
     * @param $id
     * @return array
     */
    function getRolePermissions(Request $request, $id)
    {
        try{
            $ids = AuthRolePermission::where('roleid', $id)->pluck('permissionid')->toarray();
            $item = AuthPermission::find($ids);
            return $this->success($item);
        }catch (\Exception $e){
            return $this->fail(false, $e->getMessage());
        }
    }

    /**
     * 获取所有权限
     * @param Request $request
     * @return array
     */
    function getPermissions(Request $request)
    {
        try{
            $permissions = AuthPermission::all();
            return $this->success($permissions);
        }catch (\Exception $e){
            return $this->fail(false, $e->getMessage());
        }
    }


    /**
     * get own permissions
     * @param Request $request
     * @return array
     */
    function getOwnPermissions(Request $request)
    {
        $user = Token::user();
        $userid = $user->userid;
        $permissions = AuthPermission::getPermissionsByUserID($userid);
        return $this->success($permissions);
    }

}
